package qldc.pm.tiensu.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qldc.pm.qtht.model.CoQuan;
import qldc.pm.qtht.utils.NguoiDungUtils;
import qldc.pm.tiensu.constants.TienSuConstants;
import qldc.pm.tiensu.dto.KetQuaTrichLucDTO;
import qldc.pm.tiensu.portlet.action.ReportAction;
import qldc.pm.tiensu.service.KetQuaTrichLucLocalServiceUtil;
import qldc.pm.tiensu.service.YeuCauTrichLucLocalServiceUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
import com.report.logic.Report;

public class QuanLyYeuCauTrichLuc {
	private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");

	private static Log _log = LogFactoryUtil.getLog(QuanLyYeuCauTrichLuc.class);
	
	public void capNhatKetQuaTrichLucTienSu(ActionRequest actionRequest, ActionResponse actionResponse) {
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
			
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		String cmd = ParamUtil.getString(actionRequest, "cmd");
		KetQuaTrichLucDTO ketQuaDTO = getKetQuaTrichLuc(actionRequest);	
		String soDinhDanhCaNhan = ParamUtil.getString(actionRequest, "soDinhDanhCaNhan");

		try {
			User user = PortalUtil.getUser(actionRequest);

			if(Validator.isNotNull(user)) {
				if(ketQuaDTO.getKetQuaTrichLucId() > 0) {
					KetQuaTrichLucLocalServiceUtil.updateKetQuaTrichLuc(ketQuaDTO.getKetQuaTrichLucId(), user,
							ketQuaDTO.getMaKetQuaTrichLuc(), ketQuaDTO.getYeuCauTrichLucId(), ketQuaDTO.getKinhGui(),
							ketQuaDTO.getTinhTrangAnTich(), ketQuaDTO.getThongTinAnTich(), ketQuaDTO.getToiDanh(),
							ketQuaDTO.getHinhPhatChinh(), ketQuaDTO.getHinhPhatBoSung(), ketQuaDTO.getTrinhTrangThiHanhAn(),
							ketQuaDTO.getNguoiTraCuuHoSo(), ketQuaDTO.getNgayTraCuu(),ketQuaDTO.getKetQuaSauKhiTrichLuc());

					SessionMessages.add(actionRequest, "cap-nhat-ket-qua-trich-luc-thanh-cong");
				} else {
					KetQuaTrichLucLocalServiceUtil.addKetQuaTrichLuc(user,
							ketQuaDTO.getMaKetQuaTrichLuc(), ketQuaDTO.getYeuCauTrichLucId(), ketQuaDTO.getKinhGui(),
							ketQuaDTO.getTinhTrangAnTich(), ketQuaDTO.getThongTinAnTich(), ketQuaDTO.getToiDanh(),
							ketQuaDTO.getHinhPhatChinh(), ketQuaDTO.getHinhPhatBoSung(), ketQuaDTO.getTrinhTrangThiHanhAn(),
							ketQuaDTO.getNguoiTraCuuHoSo(), ketQuaDTO.getNgayTraCuu(),ketQuaDTO.getKetQuaSauKhiTrichLuc());

					SessionMessages.add(actionRequest, "them-moi-ket-qua-trich-luc-thanh-cong");
				}
				//update yeu cau trich luc voi so dinh danh ca nhan
				long yeuCauTrichLucId = ParamUtil.getLong(actionRequest, "yeuCauTrichLucId");

				YeuCauTrichLucLocalServiceUtil.updateYeuCauTrichLucVoiSoDinhDanhCaNhan(yeuCauTrichLucId, soDinhDanhCaNhan);
			} else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}
		} catch (Exception e) {
			_log.warn(e);
			SessionErrors.add(actionRequest, "cap-nhat-ket-qua-trich-luc-khong-thanh-cong");
		}

		try {

			if (cmd.equals(TienSuConstants.IN)){
				inKetQuaTrichLuc(ketQuaDTO, actionRequest, actionResponse);				
			}
			actionResponse.sendRedirect(redirect);
		} catch (IOException e) {
			_log.warn(e);
		}
	}

	private void inKetQuaTrichLuc(KetQuaTrichLucDTO dto, ActionRequest request, ActionResponse response) throws IOException{
		HashMap<String, Object> data = ReportAction.createDataReportKetQuaTrichLuc(dto);
		PortletContext pc = request.getPortletSession().getPortletContext();
		String reportFileName = TienSuConstants.TRICH_LUC_TIEN_SU;
		String reportFilePath = pc
				.getRealPath(TienSuConstants.REPORT_TEMPLATE_FOLDER + "/"
						+ reportFileName + ".jrxml");

		File file = Report.createReport(data, Report.DOC_TYPE, reportFilePath,
				reportFileName);
		if (Validator.isNotNull(file)) {
			InputStream in = new FileInputStream(file);

			HttpServletRequest httpServletRequest = PortalUtil
					.getHttpServletRequest(request);
			HttpServletResponse httpServletResponse = PortalUtil
					.getHttpServletResponse(response);
			ServletResponseUtil.sendFile(httpServletRequest,
					httpServletResponse, reportFileName + "."
							+ Report.DOC_TYPE, in, Report.DOC_STREAM_TYPE);
		}
		
	}
	
	private KetQuaTrichLucDTO getKetQuaTrichLuc(ActionRequest actionRequest) {
		KetQuaTrichLucDTO kq = new KetQuaTrichLucDTO();
		
		int ketQuaSauKhiTrichLuc = ParamUtil.getInteger(actionRequest, "ketQuaSauKhiTrichLuc");
		kq.setKetQuaSauKhiTrichLuc(ketQuaSauKhiTrichLuc);
		long yeuCauTrichLucId = ParamUtil.getLong(actionRequest, "yeuCauTrichLucId");
		kq.setYeuCauTrichLucId(yeuCauTrichLucId);
		long ketQuaTrichLucId = ParamUtil.getLong(actionRequest, "ketQuaTrichLucId");
		kq.setKetQuaTrichLucId(ketQuaTrichLucId);

		String maKetQuaTrichLuc = ParamUtil.getString(actionRequest, "maKetQuaTrichLuc");
		kq.setMaKetQuaTrichLuc(maKetQuaTrichLuc);
		
    	String kinhGui = ParamUtil.getString(actionRequest, "kinhGui");
    	kq.setKinhGui(kinhGui);

		String tinhTrangAnTich = ParamUtil.getString(actionRequest, "tinhTrangAnTich");
		kq.setTinhTrangAnTich(tinhTrangAnTich);
		String thongTinAnTich = ParamUtil.getString(actionRequest, "thongTinAnTich");
		kq.setThongTinAnTich(thongTinAnTich);
		String toiDanh = ParamUtil.getString(actionRequest, "toiDanh");
		kq.setToiDanh(toiDanh);

		String hinhPhatChinh = ParamUtil.getString(actionRequest, "hinhPhatChinh");
		kq.setHinhPhatChinh(hinhPhatChinh);
		String hinhPhatBoSung = ParamUtil.getString(actionRequest, "hinhPhatBoSung");
		kq.setHinhPhatBoSung(hinhPhatBoSung);
		String tinhTrangThiHanhAn = ParamUtil.getString(actionRequest, "tinhTrangThiHanhAn");
		kq.setTrinhTrangThiHanhAn(tinhTrangThiHanhAn);
		
		Date ngayTraCuu = ParamUtil.getDate(actionRequest, "ngayTraCuu", DATE_FORMAT);
		kq.setNgayTraCuu(ngayTraCuu);
		String nguoiTraCuu = ParamUtil.getString(actionRequest, "nguoiTraCuu");
		kq.setNguoiTraCuuHoSo(nguoiTraCuu);
		
		Date ngayGuiYeuCau = ParamUtil.getDate(actionRequest, "ngayGuiYeuCau", DATE_FORMAT);
		String coQuanNguoiDung = ParamUtil.getString(actionRequest, "coQuanNguoiDung");
		String maPhieuTrichLuc = ParamUtil.getString(actionRequest, "maPhieuTrichLuc");
		String hoVaTenNguoiCanTrichLuc = ParamUtil.getString(actionRequest, "hoVaTenNguoiCanTrichLuc");

		kq.setCoQuanNguoiDung(coQuanNguoiDung);
		kq.setMaPhieuTrichLuc(maPhieuTrichLuc);
		kq.setNgayGuiYeuCau(ngayGuiYeuCau);
		kq.setHoVaTenNguoiCanTrichLuc(hoVaTenNguoiCanTrichLuc);
		String coQuanYeuCauTrichLuc = ParamUtil.getString(actionRequest, "coQuanYeuCauTrichLuc");

		kq.setCoQuanYeuCauTrichLuc(coQuanYeuCauTrichLuc);
		
		try {
			User user = PortalUtil.getUser(actionRequest);
			CoQuan cq = NguoiDungUtils.getCoQuanCuaNguoiDung(user.getUserId());
			if (Validator.isNotNull(cq)){
				kq.setCoQuanNguoiDung(cq.getTenCoQuan());
			}
		} catch (PortalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return kq;
	}


	public void capNhatYeuCauTrichLucTienSu(ActionRequest actionRequest, ActionResponse actionResponse) {
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

		String redirect = ParamUtil.getString(actionRequest, "redirect");

		try {
			User user = PortalUtil.getUser(actionRequest);
			if(Validator.isNotNull(user)) {
				long yeuCauTrichLucId = ParamUtil.getLong(actionRequest, "yeuCauTrichLucId");
				
				
				Date ngayCapSoCMND  = ParamUtil.getDate(actionRequest, "ngayCapSoCMND", new SimpleDateFormat("dd/MM/yyyy"));
				Date ngayCapSoHoChieu  = ParamUtil.getDate(actionRequest, "ngayCapSoHoChieu", new SimpleDateFormat("dd/MM/yyyy"));
				String noiCapSoCMND = ParamUtil.getString(actionRequest, "noiCapSoCMND");
				String noiCapSoHoChieu = ParamUtil.getString(actionRequest, "noiCapSoHoChieu");
				String soHoChieuDoiTuongTrichLuc = ParamUtil.getString(actionRequest, "soHoChieuDoiTuongTrichLuc");
				String soCMNDDoiTuongTrichLuc = ParamUtil.getString(actionRequest, "soCMNDDoiTuongTrichLuc");

				
				String hoVaTenGoiKhac = ParamUtil.getString(actionRequest, "hoVaTenNguoiYeuCauTrichLuc");
				
				
		    	String hoVaTenCha = ParamUtil.getString(actionRequest, "hoTenCha");
		    	String hoVaTenMe = ParamUtil.getString(actionRequest, "hoVaTenMe");
		    	String hoTenVoHoacChong = ParamUtil.getString(actionRequest, "hoTenVoHoacChong");
				String hoVaTenNguoiYeuCauTrichLuc = ParamUtil.getString(actionRequest, "hoVaTenNguoiYeuCauTrichLuc");
				long noiTamTruDoiTuongTrichLucTinhId = ParamUtil.getLong(actionRequest, "noiTamTruDoiTuongTrichLucTinhId");
				long noiTamTruDoiTuongTrichLucQuanHuyenId = ParamUtil.getLong(actionRequest, "noiTamTruDoiTuongTrichLucQuanHuyenId");
				long noiTamTruDoiTuongTrichLucPhuongXaId = ParamUtil.getLong(actionRequest, "noiTamTruDoiTuongTrichLucPhuongXaId");
				String noiTamTruDoiTuongTrichLucDiaChi = ParamUtil.getString(actionRequest, "noiTamTruDoiTuongTrichLucDiaChi");
				String soDinhDanhCaNhanDoiTuongTrichLuc = ParamUtil.getString(actionRequest, "soDinhDanhCaNhanDoiTuongTrichLuc");
				String hoVaTenDoiTuongTrichLuc = ParamUtil.getString(actionRequest, "hoVaTenDoiTuongTrichLuc");
				Date ngaySinhDoiTuongTrichLuc = ParamUtil.getDate(actionRequest, "ngaySinhDoiTuongTrichLuc", DATE_FORMAT);
				long noiThuongTruDoiTuongTrichLucTinhId = ParamUtil.getLong(actionRequest, "noiThuongTruDoiTuongTrichLucTinhId");
				long noiThuongTruDoiTuongTrichLucQuanHuyenId = ParamUtil.getLong(actionRequest, "noiThuongTruDoiTuongTrichLucQuanHuyenId");
				long noiThuongTruDoiTuongTrichLucPhuongXaId = ParamUtil.getLong(actionRequest, "noiThuongTruDoiTuongTrichLucPhuongXaId");
				String noiThuongTruDoiTuongTrichLucDiaChi = ParamUtil.getString(actionRequest, "noiThuongTruDoiTuongTrichLucDiaChi");
				String soSoHoKhauDoiTuongTrichLuc = ParamUtil.getString(actionRequest, "soSoHoKhauDoiTuongTrichLuc");
				String noiDungTrichLuc = ParamUtil.getString(actionRequest, "noiDungTrichLuc");
				Date ngayDeNghiTraTrichLuc = ParamUtil.getDate(actionRequest, "ngayDeNghiTraTrichLuc", DATE_FORMAT);
				String chuThich = ParamUtil.getString(actionRequest, "chuThich");
				String maYeuCauTrichLuc = ParamUtil.getString(actionRequest, "maYeuCauTrichLuc");
				String kinhGui = ParamUtil.getString(actionRequest, "kinhGui");
				String quocTich = ParamUtil.getString(actionRequest, "quocTich");
				int gioiTinh = ParamUtil.getInteger(actionRequest, "gioiTinh");
				long danToc = ParamUtil.getLong(actionRequest, "danToc");
				Date ngayLapTrichLuc = ParamUtil.getDate(actionRequest, "ngayLapTrichLuc", DATE_FORMAT);
				String coQuanYeuCau = ParamUtil.getString(actionRequest, "coQuanYeuCau");
				String noiSinh = ParamUtil.getString(actionRequest, "noiSinh");
				long noiSinhTinhId = ParamUtil.getLong(actionRequest, "noiSinhTinhId");
				long noiSinhQuanHuyenId = ParamUtil.getLong(actionRequest, "noiSinhQuanHuyenId");
				long noiSinhPhuongXaId = ParamUtil.getLong(actionRequest, "noiSinhPhuongXaId");

				if(yeuCauTrichLucId > 0) {
					YeuCauTrichLucLocalServiceUtil.updateYeuCauTrichLuc(yeuCauTrichLucId, user,
							maYeuCauTrichLuc, 
							hoVaTenNguoiYeuCauTrichLuc,
							noiTamTruDoiTuongTrichLucTinhId,
							noiTamTruDoiTuongTrichLucQuanHuyenId,
							noiTamTruDoiTuongTrichLucPhuongXaId, noiTamTruDoiTuongTrichLucDiaChi,
							soCMNDDoiTuongTrichLuc,
							soDinhDanhCaNhanDoiTuongTrichLuc,
							hoVaTenDoiTuongTrichLuc, ngaySinhDoiTuongTrichLuc,
							noiThuongTruDoiTuongTrichLucTinhId,
							noiThuongTruDoiTuongTrichLucQuanHuyenId,
							noiThuongTruDoiTuongTrichLucPhuongXaId,
							noiThuongTruDoiTuongTrichLucDiaChi,
							soSoHoKhauDoiTuongTrichLuc, noiDungTrichLuc,
							ngayDeNghiTraTrichLuc, chuThich, ngayCapSoCMND, hoVaTenGoiKhac,
					    	hoVaTenCha,
					    	hoVaTenMe,
					    	hoTenVoHoacChong,kinhGui,quocTich,danToc,gioiTinh,ngayLapTrichLuc,coQuanYeuCau,noiCapSoCMND,
					    	soHoChieuDoiTuongTrichLuc, noiCapSoHoChieu, ngayCapSoHoChieu,noiSinh,noiSinhTinhId, noiSinhQuanHuyenId, noiSinhPhuongXaId);

					SessionMessages.add(actionRequest, "cap-nhat-yeu-cau-trich-luc-thanh-cong");
				} else {
					YeuCauTrichLucLocalServiceUtil.addYeuCauTrichLuc(user, maYeuCauTrichLuc, 
							hoVaTenNguoiYeuCauTrichLuc,
							noiTamTruDoiTuongTrichLucTinhId,
							noiTamTruDoiTuongTrichLucQuanHuyenId,
							noiTamTruDoiTuongTrichLucPhuongXaId, noiTamTruDoiTuongTrichLucDiaChi,
							soCMNDDoiTuongTrichLuc,
							soDinhDanhCaNhanDoiTuongTrichLuc,
							hoVaTenDoiTuongTrichLuc, ngaySinhDoiTuongTrichLuc,
							noiThuongTruDoiTuongTrichLucTinhId,
							noiThuongTruDoiTuongTrichLucQuanHuyenId,
							noiThuongTruDoiTuongTrichLucPhuongXaId,
							noiThuongTruDoiTuongTrichLucDiaChi,
							soSoHoKhauDoiTuongTrichLuc, noiDungTrichLuc,
							ngayDeNghiTraTrichLuc, chuThich, ngayCapSoCMND, hoVaTenGoiKhac,
					    	hoVaTenCha,
					    	hoVaTenMe,
					    	hoTenVoHoacChong,kinhGui,quocTich,danToc,gioiTinh,ngayLapTrichLuc,coQuanYeuCau,noiCapSoCMND, soHoChieuDoiTuongTrichLuc, 
					    	noiCapSoHoChieu, ngayCapSoHoChieu,noiSinh,noiSinhTinhId, noiSinhQuanHuyenId, noiSinhPhuongXaId);

					SessionMessages.add(actionRequest, "them-moi-yeu-cau-trich-luc-thanh-cong");
				}
			} else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}
		} catch (Exception e) {
			_log.warn(e);
			SessionErrors.add(actionRequest, "cap-nhat-yeu-cau-trich-luc-khong-thanh-cong");
		}

		try {
			actionResponse.sendRedirect(redirect);
		} catch (IOException e) {
			_log.warn(e);
		}
	}
	public void xoaYeuCauTrichLucTienSu(ActionRequest actionRequest, ActionResponse actionResponse) {
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

		String redirect = ParamUtil.getString(actionRequest, "redirect");

		User user;
		try {
			user = PortalUtil.getUser(actionRequest);

			if(Validator.isNotNull(user)) {
				long yeuCauTrichLucId = ParamUtil.getLong(actionRequest, "yeuCauTrichLucId");

				YeuCauTrichLucLocalServiceUtil.deleteYeuCauTrichLuc(yeuCauTrichLucId);

				SessionMessages.add(actionRequest, "xoa-yeu-cau-trich-luc-thanh-cong");
			} else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}
		} catch (Exception e) {
			_log.warn(e);
			SessionErrors.add(actionRequest, "xoa-yeu-cau-trich-luc-khong-thanh-cong");
		}

		try {
			actionResponse.sendRedirect(redirect);
		} catch (IOException e) {
			_log.warn(e);
		}
	}
}
